package com.focusai.efairy.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.focusai.efairy.EFApplication;
import com.focusai.efairy.R;
import com.focusai.efairy.ui.Ipresenter.ILoginPresenter;
import com.focusai.efairy.ui.activity.global.UserProtocolActivity;
import com.focusai.efairy.ui.base.BaseFragmentActivity;
import com.focusai.efairy.ui.iview.ILogin;
import com.focusai.efairy.ui.presenter.LoginPresenter;
import com.focusai.efairy.utils.ActivityUtils;
import com.focusai.efairy.utils.AndroidUtils;
import com.focusai.efairy.utils.PreferenceKeys;
import com.focusai.efairy.utils.PreferencesUtil;
import com.focusai.efairy.utils.StringUtils;

/**
 * Created by luo_guangwen on 2017/7/12.
 */

public class PasswdLoginActivity extends BaseFragmentActivity implements ILogin, View.OnClickListener{
    private static final int REQUEST_CODE = 0x100;
    private ILoginPresenter mPresenter;
    private EditText mPhoneEdit;
    private EditText mPasswdEdit;
    private Button mBtnLoginByCode;
    private Button mBtnStartLogin;
    private String mPhone;//, password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_passwd_login);
        setCenterTitleName(R.string.passwd_login);
        initView();
        bindEvents();
        loadData();
        PreferencesUtil.getInstance().putInt(PreferencesUtil.PrefType.system, PreferenceKeys.OPEN_APP_FIRST, AndroidUtils.getVersionCode(getContext()));
    }

    @Override
    protected void initView() {
        findView(R.id.btn_get_code).setOnClickListener(this);
        TextView textView = findView(R.id.tv_depaisen_service);
        textView.setOnClickListener(this);
        if (EFApplication.isEfairyApp()){
            textView.setText(R.string.service_efairy);
        }else if (EFApplication.isCciotApp()){
            textView.setText(R.string.service_cciot);
        }
        mBtnStartLogin = findView(R.id.btn_login_next);
        mBtnStartLogin.setOnClickListener(this);
        mPhoneEdit = findView(R.id.et_number);
        mPasswdEdit = findView(R.id.et_code);
        mBtnLoginByCode = findView(R.id.btn_get_code);
        mBtnLoginByCode.setOnClickListener(this);
    }

    @Override
    protected void bindEvents() {
        mBtnStartLogin.setEnabled(false);
        String string = PreferencesUtil.getInstance().getString(PreferencesUtil.PrefType.system, PreferenceKeys.LAST_LOGIN_USER_PHONE, "");
        mPhoneEdit.setText(string);
        mPasswdEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (editable.length() <= 0) {
                    mBtnStartLogin.setEnabled(false);
                } else {
                    if (mPasswdEdit.getText().length() <= 0) {
                        mBtnStartLogin.setEnabled(false);
                    } else {
                        mBtnStartLogin.setEnabled(true);
                    }

                }
            }
        });
        mPhoneEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                StringUtils.setEditInputPhoneSeparator(mPhoneEdit);
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() <= 0) {
                    mBtnStartLogin.setEnabled(false);
                } else {
                    if (mPasswdEdit.getText().length() <= 0) {
                        mBtnStartLogin.setEnabled(false);
                    } else {
                        mBtnStartLogin.setEnabled(true);
                    }

                }
            }
        });
    }

    @Override
    protected void loadData() {
        mPresenter = new LoginPresenter(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_get_code:
                Intent intent1 = new Intent(getContext(),RegisterActivity.class);
                startActivityForResult(intent1,REQUEST_CODE);
                break;
            case R.id.btn_login_next:
                if (validate()) {
                    if (validateCode()) {
                        showSingleDialog("正在登录..");
                        mPresenter.loginUsePasswd(mPhone,mPasswdEdit.getText().toString());
                    }
                }
                break;
            case R.id.tv_depaisen_service:
                Intent intent = new Intent(getContext(), UserProtocolActivity.class);
                startActivity(intent);
                break;
            default:
                break;
        }
    }

    private boolean validate() {
        mPhone = null;
        if (StringUtils.isNull(mPhoneEdit)) {
            showToast("手机号码不能为空");
            mPhoneEdit.requestFocus();
            return false;
        }
        String phone = StringUtils.getEditInputTextWithoutSeparator(mPhoneEdit);
        if (!StringUtils.isBlank(phone)) {
            mPhone = phone;
            return true;
        } else {
            showToast("手机号码不合法");
        }
        return false;
    }

    private boolean validateCode() {
        if (StringUtils.isNull(mPasswdEdit)) {
            showToast("密码不能为空");
            mPasswdEdit.requestFocus();
            return false;
        }
        return true;
    }

    @Override
    public void loginFinish(boolean isOk, String passwd, String msg) {
        closeSingleDialog();
        if (isOk) {
            ActivityUtils.startMainHomeActivity(PasswdLoginActivity.this);
            finish();
        }else {
            showToastOnUiThread(msg);
        }
    }

    @Override
    public void getCodeFinish(boolean isOk, String code, String msg) {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == REQUEST_CODE){
            finish();
        }
    }
}
